package itdep.ui.components.profile;

import ga.tapestry.commonlib.base.components.AbstractComponent;
import ga.tapestry.commonlib.components.Details;
import ga.tapestry.internal.SelectModelBuilder;
import itdep.domain.profile.IdentityCardType;
import itdep.domain.profile.Profile;
import itdep.services.profile.IdentityCardTypeService;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.ioc.annotations.Inject;

import java.util.List;

/**
 * @author Ivan Khalopik
 * @version $Revision: 55 $ $Date: 2010-07-06 13:54:59 +0000 (Tue, 06 Jul 2010) $
 */
public class ProfileEditor extends AbstractComponent{

	@Parameter(required = true)
	private Profile object;

	@Parameter
	private boolean editable;

	@Inject
	private IdentityCardTypeService identityCardTypeService;

	@Inject
	private SelectModelBuilder selectModelBuilder;

	@Component(
			parameters = {
					"overrides=overrides",
					"object=object",
					"skin=inherit:skin",
					"editable=editable"})
	private Details generalDetails;

	@Component(
			parameters = {
					"overrides=overrides",
					"object=object?.identityCard",
					"skin=inherit:skin",
					"editable=editable"})
	private Details identityCardDetails;

	private SelectModel cardTypeModel;

	public Profile getObject() {
		return object;
	}

	public boolean isEditable() {
		return editable;
	}

	public SelectModel getCardTypeModel() {
		if (cardTypeModel == null) {
			final List<IdentityCardType> cardTypes = identityCardTypeService.getEntities();
			cardTypeModel = selectModelBuilder.buildFormatted(IdentityCardType.class, cardTypes, "%s", "this:description");
		}
		return cardTypeModel;
	}
}
